﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YoloImageDetect
{
    public class DialogHelper
    {
        public const string FILTER_ALL = "Files (*.*)|*.*";
        public const string FILTER_EXT = "Files (*.{0})|*.{1}";
        public const string FILTER_EXE = "Files (*.exe)|*.exe";
        public const string FILTER_WLP = "Files (*.wlp)|*.wlp";
        public const string FILTER_WAYLINE = "Files (*.kml)|*.kml";
        public const string FILTER_TIF = "Files (*.tif)|*.tif";
        public const string IR_FILTER_TIF = "Files (*_T.tif)|*_T.tif";
        public const string VIS_FILTER_TIF = "Files (*_V.tif)|*_V.tif";
        public const string FILTER_WORD = "DOCX (*.docx)|*.docx|DOC (*.doc)|*.doc";
        public const string FILTER_REPORT = "DOCX (*.docx)|*.docx|DOC (*.doc)|*.doc|PDF (*.pdf)|*.pdf";
        public const string FILTER_XLSX = "Files (*.xlsx)|*.xlsx";
        public const string FILTER_GEOJSON = "Files (*.geojson)|*.geojson";
        public const string FILTER_MODEL_ONNX = "ONNX (*.onnx)|*.onnx";
        public const string FILTER_MODEL_YAML = "ONNX (*.yaml)|*.yaml";
        public const string FILTER_MODEL_ONNX_YAML = "ONNX (*.onnx)|*.onnx|YALM (*.yaml)|*.yaml";

      
        public static string SelectFilePath(string fileFilter, bool multiselectEnable = false, string title = "")
        {
            fileFilter = string.IsNullOrEmpty(fileFilter) ? FILTER_ALL : fileFilter;
            string path = string.Empty;
            var openFileDialog = new OpenFileDialog()
            {
                Filter = fileFilter,
                // 是否允许多选
                Multiselect = multiselectEnable
            };
            if (!string.IsNullOrEmpty(title))
            {
                openFileDialog.Title = title;
            }
            var result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                path = openFileDialog.FileName;
            }
            return path;
        }

        public static string SelectMultiFilePath(string fileFilter)
        {
            fileFilter = string.IsNullOrEmpty(fileFilter) ? FILTER_ALL : fileFilter;
            string path = string.Empty;
            var openFileDialog = new OpenFileDialog()
            {
                Filter = fileFilter
            };
            openFileDialog.Multiselect = true;
            var result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                foreach (string fileName in openFileDialog.FileNames)
                {
                    if (string.IsNullOrEmpty(path))
                    {
                        path = fileName;
                    }
                    else
                    {
                        path = path + ";" + fileName;

                    }

                }
            }
            return path;
        }

        public static string SelectPath(string description = "请选择一个目录")
        {
            string path = string.Empty;
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.Description = description;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                path = fbd.SelectedPath;
            }
            return path;
        }

        public static void SaveFile(string ext, object obj)
        {
            string localFilePath = "", fileNameExt = "", newFileName = "", FilePath = "";
            string fileFilter = string.IsNullOrEmpty(ext) ? FILTER_ALL : string.Format(FILTER_EXT, ext, ext);
            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog() { Filter = fileFilter };
            // 默认文件名
            sfd.FileName = "导出" + DateTime.Now.ToString("yyyyMMddHHmmss");
            // 默认扩展名
            sfd.DefaultExt = "wlp";
            // 获取或设置一个值，该值指示如果用户省略扩展名，文件对话框是否自动在文件名中添加扩展名。（可以不设置）
            sfd.AddExtension = true;
            // 默认文件类型显示顺序
            sfd.FilterIndex = 2;

            //保存对话框是否记忆上次打开的目录
            sfd.RestoreDirectory = true;

            // Show save file dialog box
            DialogResult result = sfd.ShowDialog();
            //点了保存按钮进入
            if (result == DialogResult.OK)
            {
                //获得文件路径
                localFilePath = sfd.FileName.ToString();
                //获取文件名，不带路径
                fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
                //获取文件路径，不带文件名
                FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));//在文件名里加字符
                //saveFileDialog.FileName.Insert(1,"dameng");
                //为用户使用 SaveFileDialog 选定的文件名创建读/写文件流。
                System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件
                // 

                System.IO.StreamWriter writer = new System.IO.StreamWriter(fs, Encoding.UTF8);

                string type = obj.GetType().Name;
                string JsonString = type == "String" ? obj.ToString() : Newtonsoft.Json.JsonConvert.SerializeObject(obj);

                writer.Write(JsonString);

                writer.Close();
                fs.Close();
                fs.Dispose();
            }

        }

        public static string saveFileDialog(string fileFilter, string defaultFileName, string title = "保存文件")
        {
            fileFilter = string.IsNullOrEmpty(fileFilter) ? FILTER_ALL : fileFilter;
            string path = string.Empty;
            var saveFileDialog = new SaveFileDialog()
            {
                Filter = fileFilter,
                Title = title,
                FileName = defaultFileName
            };
            var result = saveFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                path = saveFileDialog.FileName;
            }
            return path;
        }
    }
}
